<?php
// This file is part of Meta AGM grading method - https://code.google.com/p/oasychev-moodle-plugins
//
// Meta AGM grading method is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Meta AGM is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Meta AGM.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Meta AGM renderer.
 *
 * @package    gradingform_metaagm
 * @author     Nikita Kalinin <nixorv@gmail.com>
 * @copyright  2014 Oleg Sychev (Volgograd State Technical University) <oasychev@gmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace gradingform_metaagm\output;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot.'/grade/grading/lib.php');

/**
 * Meta AGM renderer class.
 *
 * @package    gradingform_metaagm
 * @author     Nikita Kalinin <nixorv@gmail.com>
 * @copyright  2014 Oleg Sychev (Volgograd State Technical University) <oasychev@gmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class renderer extends \plugin_renderer_base {
    
    /**
     * Renders the grading form preview.
     * @param array $metacriteria Metacriterions array.
     * @return string HTML.
     */
    public function gradingform_preview(array $metacriteria) {
        $html = '';
        $readynumber = 0;
        foreach ($metacriteria as $mc) {
            $mcmanager = get_grading_manager($mc->subareaid);
            $mccontroller = $mcmanager->get_controller($mc->activemethod);
            
            if ($mccontroller->is_form_available()) {
                $readynumber++;
                
                $html .= \html_writer::tag('h4', $mc->name);                
                $html .= $mccontroller->render_preview($this->page);
            }
        }
        
        if ($readynumber == 0) {
            $html .= \html_writer::tag('h4', get_string('noavailablemetacriteria', 'gradingform_metaagm'));
            $html .= get_string('noavailablemetacriteriahint', 'gradingform_metaagm');
        }
        
        return $html;
    }

    /**
     * Renders the grading form (grade element).
     * @param array $metacriteria
     * @param \gradingform_metaagm_instance $metainstance
     * @param $gradingformelement
     * @return string HTML.
     */
    public function gradingform(array $metacriteria, \gradingform_metaagm_instance $metainstance, $gradingformelement) {
        global $USER;
        $html = '';
        
        $html .= \html_writer::empty_tag('input', array('name' => $gradingformelement->getName() . '[]', 'type' => 'hidden', 'value' => 0));
        
        foreach ($metacriteria as $mc) {
            $mcmanager = get_grading_manager($mc->subareaid);
            $mccontroller = $mcmanager->get_controller($mc->activemethod);
    
            if ($mccontroller->is_form_available()) {
                // Begin Dirty Hack. In order we need the $mform access to do it correctly.
                $mcelement = clone $gradingformelement;
                $mcelement->setName('_metaagm_advancedgrading' . $mc->id);
                $mcelement->setValue(null);
                // End Dirty Hack.
                $instance = $mccontroller->get_or_create_instance(0, $USER->id, $metainstance->get_data('itemid'));
                $html .= \html_writer::tag('h4', $mc->name);
                $html .= $instance->render_grading_element($this->page, $mcelement);
            }
        }
    
        return $html;
    }
}
